package com.li.test.base.arthas;

import java.util.HashSet;
import java.util.concurrent.TimeUnit;

public class Arthas {
    private static HashSet hashset = new HashSet<>();

    public static void main(String[] args) {
       //模拟cpu过高
       cpuhigh();
       //模拟线程死锁
        deadThread();
        //不断向hashset加数据
        addHashSetThread();
    }
    public static  void addHashSetThread(){
        //初始化常量
        new Thread(()->{
            int count = 0;
            while (true){
                try {
                hashset.add("count"+count);

                    TimeUnit.SECONDS.sleep(1);
                    count++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public static  void cpuhigh(){
        new Thread(()->{
            while (true){

            }
        }).start();
    }
    /**
     * 死锁
     */
    private static void deadThread(){
        //创建资源
        Object resourceA = new Object();
        Object resourceB = new Object();
        //创建线程
        Thread threadA = new Thread(()->{
           synchronized (resourceA){
               System.out.println(Thread.currentThread()+"Get ResourceA");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread()+"waitting Get ResourceB");
               synchronized (resourceB){
                   System.out.println(Thread.currentThread()+"Get ResourceB");
               }

           }
        });
        Thread threadB = new Thread(()->{
            synchronized (resourceB){
                System.out.println(Thread.currentThread()+"Get ResourceB");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+"waitting Get ResourceA");
                synchronized (resourceA){
                    System.out.println(Thread.currentThread()+"Get ResourceA");
                }

            }
        });
        threadA.start();
        threadB.start();


    }
}
